import Header from "../../components/Header";
import {Row, Col, Dropdown} from "../../rsuite";
import {Link} from "react-router";
import {connect} from "react-redux";
import NoData from "../../components/nodata";
import Search from "../../components/search";
import Panel from "../../elements/Panel";
import Pagination from "../../components/Pagination/";
import Config from "../../libs/config";
import {SUBJECT, PAGE_SUBJECT} from "../../actions/subject";
import {fetch, fetch_remove, enable_delete_btn, selectAllItem, selectItem} from "../../actions";
import {md5Url, URL} from '../../libs/Url';

const News = React.createClass({
    componentDidMount() {
        const {dispatch} = this.props;
        dispatch(fetch({url: URL.TEACH_SCHEDULE_FIND}));
    },
    search(keyword){
        const {dispatch} = this.props;
        dispatch(fetch({url: URL.TEACH_SCHEDULE_FIND, keyword}))
    },
    handleSelect(eventKey){
        const {dispatch} = this.props;
        dispatch(fetch({url: URL.TEACH_SCHEDULE_FIND, type: eventKey}))
    },
    render(){
        const self = this;
        const {dispatch} = this.props;
        const data = self.props[md5Url.TEACH_SCHEDULE_FIND] || {};
        (!data.list && (data.list = []));
        return (
            <Panel>
                <Header>教学计划</Header>
                <div className="table-list-head row">
                    <Col xs={6} md={6}>
                        <Link className='btn btn-primary btn-sm' to={Config.MENUS_ABSOLUTE.TEACH_SCHEDULE_ADD}>
                            <i className="fa fa-plus"></i> 新建
                        </Link>
                        <button disabled={(self.props[md5Url.TEACH_SCHEDULE_FIND].enable_delete_btn == 0 )}
                                onClick={(e)=> {
                                    if (confirm('确认删除？')) {
                                        var tmp = _.filter(data.list, (item)=> {
                                            return item.checked;
                                        });
                                        dispatch(fetch_remove({
                                            url: URL.TEACH_SCHEDULE_DELETE,
                                            target: md5Url.TEACH_SCHEDULE_FIND,
                                            ids: _.pluck(_.filter(data.list, (item)=> {
                                                return item.checked;
                                            }), 'id').join(',')
                                        }))
                                    }
                                }}
                                className="btn btn-default btn-sm"
                        >删除
                        </button>
                    </Col>
                    <Col xs={6} md={3} mdOffset={3}>
                        <Search submit={this.search}/>
                    </Col>
                </div>
                <Row>

                    <Col xs={12} md={12}>
                        <table className="table table-bordered table-striped general-table">
                            <thead>
                            <tr>
                                <th style={{width: '50px'}}>
                                    <input type="checkbox" onClick={(e)=> {
                                        const self = this;
                                        dispatch(selectAllItem(md5Url.TEACH_SCHEDULE_FIND, e.target.checked));
                                        dispatch(enable_delete_btn(PAGE_SUBJECT.TEACH_SCHEDULE, {
                                            num: (e.target.checked ? self.props[md5Url.TEACH_SCHEDULE_FIND].list.length : 0),
                                            all: true
                                        }));
                                    }}/>
                                </th>
                                <th style={{width: '60px'}}>NO.</th>
                                <th>
                                    教学计划总纲标题
                                </th>
                                <th>
                                    课时安排数量
                                </th>
                                <th>
                                    <Dropdown size="xs" shape='default' activeKey="C" title="班级"
                                              onSelect={self.handleSelect} select>
                                        {data.list.map(function (item, index) {
                                            return <Dropdown.Item
                                                key={index}
                                                eventKey={item.id}>
                                                {item.name}
                                            </Dropdown.Item>
                                        })}
                                    </Dropdown>
                                </th>
                                <th>时间</th>
                                <th style={{width: '120px'}}>操作</th>
                            </tr>
                            </thead>
                            <tbody>
                            {
                                data.list.map((item, index)=> {
                                        item.index = index;
                                        return <tr key={index}>
                                            <td>
                                                <input type="checkbox" checked={(item.checked ? 'checked' : '')}
                                                       onClick={(e)=> {
                                                           dispatch(selectItem(SUBJECT.NEWS, {
                                                               index: index,
                                                               checked: e.target.checked
                                                           }));
                                                           dispatch(enable_delete_btn(PAGE_SUBJECT.TEACH_SCHEDULE,
                                                               {num: (e.target.checked ? 1 : -1)}));
                                                       }}
                                                />
                                            </td>
                                            <td className="hidden-phone">{index + 1}</td>
                                            <td><Link to={
                                                Config.MENUS_ABSOLUTE.TEACH_SCHEDULE_DETAIL.replace(':id', item.id)
                                            }>{item.name}</Link></td>
                                            <td>{item.courseCount}</td>
                                            <td>{item.class && item.class.name}</td>
                                            <td>{Moment(item.intime).format('YYYY-MM-DD HH:mm:SS')}</td>
                                            <td>
                                                <Link
                                                    to={
                                                        Config.MENUS_ABSOLUTE.TEACH_SCHEDULE_DETAIL.replace(':id', item.id)
                                                    }
                                                    className="btn btn-default btn-xs"
                                                    type="button">
                                                    <i className="fa fa-search-plus"></i>
                                                </Link>
                                                <Link
                                                    to={
                                                        Config.MENUS_ABSOLUTE.TEACH_SCHEDULE_UPDATE.replace(':id', item.id)
                                                    }
                                                    className="btn btn-default btn-xs" type="button">
                                                    <i className="fa fa-pencil"></i>
                                                </Link>
                                                <button
                                                    className="btn btn-default btn-xs"
                                                    onClick={
                                                        ()=> {
                                                            if (confirm('确认删除？')) {
                                                                dispatch(fetch_remove({
                                                                    url: URL.TEACH_SCHEDULE_DELETE,
                                                                    target: md5Url.TEACH_SCHEDULE_FIND,
                                                                    ids: item.id,
                                                                    index: index
                                                                }))
                                                                if (item.checked) {
                                                                    dispatch(enable_delete_btn(md5Url.TEACH_SCHEDULE_FIND, {num: -1}))
                                                                }
                                                            }
                                                        }
                                                    }
                                                    type="button">
                                                    <i className="fa fa-times"></i>
                                                </button>
                                            </td>
                                        </tr>
                                    }
                                )
                            }
                            </tbody>
                        </table>
                        {
                            data.list.length ? '' : <NoData />
                        }
                        {
                            (data.list.length ? <Pagination
                                onselect={(num)=> {
                                    dispatch(fetch({
                                        url: URL.TEACH_SCHEDULE_FIND,
                                        currentPage: num
                                    }))
                                }}
                                page={data.page || 0}
                                pages={data.pages || 0}
                            /> : '' )
                        }

                    </Col>
                </Row >
            </Panel>
        )
    }
});
function select(state) {
    return {
        [md5Url.TEACH_SCHEDULE_FIND]: state.fetchBySubject[md5Url.TEACH_SCHEDULE_FIND],
        [PAGE_SUBJECT.TEACH_SCHEDULE]: state.pageBySubject[PAGE_SUBJECT.TEACH_SCHEDULE],
    }
}
export default connect(select)(News)